package com.joinus.server.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.joinus.server.constant.Constant;
import com.joinus.server.entity.User;
import com.joinus.server.tools.Tools;

/**
 * 用户数据库操作层<br>
 * @author
 * <ul> 
 * <li><a href="mailto:qipenghui3056@gmail.com">henrybit</a></li>
 * </ul>
 * @version 1.0
 * @since 2013-4-4
 */
public class UsersDao extends BaseDao{
	/**用户DAO单例*/
	private final static UsersDao userDao = new UsersDao();
	/**插入新用户*/
	private final static String INSERT_NEW_USER = "insert into users (id,name,password,phoneNo,deviceNo,picUrl,sex,create_time,update_time) values ";
	/**查询用户信息*/
	private final static String QUERY_USER = "select * from users ";
	
	private UsersDao() {
	}
	
	/**
	 * 用户单例<br>
	 * @return UsersDao 用户Dao
	 */
	public static UsersDao getInstance() {
		return userDao;
	}
	
	/**
	 * 获取一批指定用户ID的信息<br>
	 * @param uidList 用户ID列表
	 * @return HashMap 用户详细信息列表
	 */
	public List<HashMap<String, String>> getUserInfoMap(List<String> uidList) {
		List<HashMap<String, String>> infoList = new ArrayList<HashMap<String, String>>();
		List<User> userList = this.getUserInfo(uidList);
		for(int i=0; userList!=null&&i<userList.size(); i++) {
			User user = userList.get(i);
			HashMap<String, String> userMap = new HashMap<String, String>();
			userMap.put("id", user.getId());
			userMap.put("name", user.getName());
			userMap.put("phoneNo", user.getPhoneNo());
			userMap.put("deviceNo", user.getDeviceNo());
			userMap.put("picUrl", user.getPicUrl());
			userMap.put("sex", String.valueOf(user.getSex()));
			infoList.add(userMap);
		}
		return infoList;
	}
	
	/**
	 * 获取一批指定用户ID的信息<br>
	 * @param uidList 用户ID列表
	 * @return List 用户详细信息列表
	 */
	public List<User> getUserInfo(List<String> uidList) {
		List<User> userList = new ArrayList<User>();
		
		StringBuilder querys = new StringBuilder();
		for(int i=0; uidList!=null&&i<uidList.size(); i++) {
			querys.append("'").append(uidList.get(i)).append("'");
			if(i != uidList.size()-1)
				querys.append(",");
		}
		
		StringBuilder sql = new StringBuilder(QUERY_USER);
		sql.append(" where id in (").append(querys.toString()).append(")");
		
		ResultSet rs = super.query(sql.toString());
		try {
			while(rs.next()) {
				String id = rs.getString("id");
				String name = rs.getString("name");
				String phoneNo = rs.getString("phoneNo");
				String deviceNo = rs.getString("deviceNo");
				String picUrl = rs.getString("picUrl");
				int sex = rs.getInt("sex");
				
				User user = new User();
				user.setId(id);
				user.setName(name);
				user.setPhoneNo(phoneNo);
				user.setDeviceNo(deviceNo);
				user.setPicUrl(picUrl);
				user.setSex(sex);
				
				userList.add(user);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return userList;
	}
	
	/**
	 * 创建一个新用户<br>
	 * @param id 新用户ID(varchar30)
	 * @param name 新用户名(varchar512)
	 * @param phoneNo 电话号码(varchar45)
	 * @param deviceNo 设备ID(varchar45)
	 * @param password 密码(varchar45)
	 * @return id-如果创建成功，返回生成的用户ID，否则返回NULL.
	 */
	public String createNewUser(String id, String name, String phoneNo, String deviceNo, String password) {
		return this.createNewUser(id, name, password, phoneNo, deviceNo, null, null, null, null);
	}
	
	/**
	 * 创建一个新用户<br>
	 * @param id 新用户ID(varchar30)
	 * @param name 新用户名(varchar512)
	 * @param password 密码(varchar45)
	 * @param phoneNo 电话号码(varchar45)
	 * @param deviceNo 设备ID(varchar45)
	 * @param picUrl 头像URL(varchar500)
	 * @param sex 性别(int)
	 * @param createTime createTime(DateTime)
	 * @param updateTime createTime(DateTIme)
	 * @return id-如果创建成功，返回生成的用户ID，否则返回NULL.
	 */
	public String createNewUser(String id, String name, String password, String phoneNo, String deviceNo, String picUrl, String sex, String createTime, String updateTime) {
		StringBuilder sql = new StringBuilder(INSERT_NEW_USER);
		if(id==null || id.isEmpty()) {
			id = Tools.createNewUid();
		}
		sql.append("('").append(id).append("',");
		
		if(password == null)
			password = "";
		sql.append("'").append(password).append("',");
		
		if(phoneNo == null)
			phoneNo = "";
		sql.append("'").append(phoneNo).append("',");
		
		if(deviceNo == null)
			deviceNo = "";
		sql.append("'").append(deviceNo).append("',");
		
		if(picUrl == null)
			picUrl = "";
		sql.append("'").append(picUrl).append("',");
		
		if(sex == null)
			sex = "1";
		sql.append(sex).append(",");
		
		Date now = new Date();
		if(createTime == null) {
			createTime = Tools.getDateFormat(now, Constant.DATE_YYYY_MM_DD_HH_MM_SS);
		}
		if(updateTime == null) {
			updateTime = Tools.getDateFormat(now, Constant.DATE_YYYY_MM_DD_HH_MM_SS);
		}
		sql.append("'").append(createTime).append("',");
		sql.append("'").append(updateTime).append("'");
		
		sql.append(")");
		
		boolean isSuccess = super.insertNotAutoIncreateMent(sql.toString());
		if(isSuccess)
			return id;
		else
			return null;
	}
	
	/**
	 * 获取用户信息<br>
	 * @param phoneNo 手机号
	 * @return User-用户信息
	 */
	public User getUserInfoByPhoneNo(String phoneNo) {
		User user = null;
		StringBuilder sql = new StringBuilder(QUERY_USER);
		sql.append(" where phoneNo='").append(phoneNo).append("'");
		ResultSet resultSet = super.query(sql.toString());
		
		try {
			resultSet.next();
			user = prepareData(resultSet);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return user;
	}
	
	/**
	 * 获取用户信息<br>
	 * @param uid 用户ID
	 * @return User-用户信息
	 */
	public User getUserInfo(String uid) {
		User user = null;
		StringBuilder sql = new StringBuilder(QUERY_USER);
		sql.append(" where id='").append(uid).append("'");
		System.out.println("query sql : "+sql.toString());
		ResultSet resultSet = super.query(sql.toString());
		
		try {
			resultSet.next();
			user = prepareData(resultSet);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return user;
	}
	
	/**
	 * 从结果集中取出用户数据<br>
	 * @param resultSet 查询结果集
	 * @return user-用户信息实体
	 * @throws Exception
	 */
	private User prepareData(ResultSet resultSet) throws Exception {
		String id = resultSet.getString("id");
		String name = resultSet.getString("name");
		String password = resultSet.getString("password");
		String phoneNo = resultSet.getString("phoneNo");
		String deviceNo = resultSet.getString("deviceNo");
		int sex = resultSet.getInt("sex");
		String picUrl = resultSet.getString("picUrl");
		Date createTime = resultSet.getDate("create_time");
		Date updateTime = resultSet.getDate("update_time");
		
		User user = new User();
		user.setId(id);
		user.setName(name);
		user.setPassword(password);
		user.setPhoneNo(phoneNo);
		user.setDeviceNo(deviceNo);
		user.setSex(sex);
		user.setPicUrl(picUrl);
		user.setCreateTime(createTime);
		user.setCreateTimeLong(createTime.getTime());
		user.setUpdateTime(updateTime);
		user.setUpdateTimeLong(updateTime.getTime());
		
		return user;
	}
}
